package com.A1908.servlet;

import com.A1908.entity.AttendPeopleInfo;
import com.A1908.entity.FeastInfo;
import com.A1908.service.AttendPeopleInfoService;
import com.A1908.service.impl.AttendPeopleServiceImpl;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(urlPatterns = {"/attendPeople"})
public class AttendPeopleServlet extends HttpServlet {
    AttendPeopleInfoService peopleInfoService = new AttendPeopleServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        String opr = request.getParameter("opr");
        PrintWriter out = response.getWriter();

        if ("getAttendPeople".equals(opr)) {
            String feastId = request.getParameter("feastId");
            FeastInfo info = new FeastInfo();
            info.setId(Integer.parseInt(feastId));

            List<AttendPeopleInfo> list = peopleInfoService.findAttendPeopleInfoByFeast(info);
            out.print(JSON.toJSONString(list));
        } else if ("add".equals(opr)) {
            String aName = request.getParameter("aName");
            String aDate = request.getParameter("aDate");
            String feastId = request.getParameter("feast");
            String aCost = request.getParameter("aCost");
            String aRelation = request.getParameter("aRelation");
            String aNote = request.getParameter("aNote");

            AttendPeopleInfo attendPeopleInfo = new AttendPeopleInfo();
            attendPeopleInfo.setAttendPeople(aName);
            attendPeopleInfo.setAttendDate(aDate);
            attendPeopleInfo.setFeastID(Integer.parseInt(feastId));
            attendPeopleInfo.setGift(Integer.parseInt(aCost));
            attendPeopleInfo.setRelationID(Integer.parseInt(aRelation));
            attendPeopleInfo.setNote(aNote);

            int count = peopleInfoService.addAttendPeople(attendPeopleInfo);
            if (count > 0) {
                out.print("true");
            } else {
                out.print("false");
            }
        } else if ("calcGift".equals(opr)) {
            String feastId = request.getParameter("feastId");
            FeastInfo info = new FeastInfo();
            info.setId(Integer.parseInt(feastId));
            double sum = peopleInfoService.totalGift(info);
            out.print(sum);
        } else if ("editCell".equals(opr)) {
            String field = request.getParameter("field");
            String value = request.getParameter("value");
            int id = Integer.parseInt(request.getParameter("id"));

            int count = peopleInfoService.CellEdit(id, field, value);
            if (count > 0) {
                out.print("true");
            } else {
                out.print("false");
            }
        } else if ("edit".equals(opr)) {
            String id = request.getParameter("id");
            String name = request.getParameter("name");
            String date = request.getParameter("date");
            String gift = request.getParameter("gift");
            String note = request.getParameter("note");
            String rId = request.getParameter("rId");
            String feastId = request.getParameter("feastId");

        }else if ("del".equals(opr)){
            String id = request.getParameter("id");
            AttendPeopleInfo attendPeopleInfo = new AttendPeopleInfo();
            attendPeopleInfo.setId(Integer.parseInt(id));
            int count = peopleInfoService.delAttendPeople(attendPeopleInfo);
            if (count > 0) {
                out.print("true");
            } else {
                out.print("false");
            }
        }
        out.close();
    }
}
